﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cms.Domain;

namespace Cms.Tests.Unit.Domain
{
    [TestClass]
    public class FolderTests
    {
        [TestMethod]
        public void IsInCategory_GrandParentIsAssociated_Success()
        {
            Folder a = new Folder();
            Folder b = new Folder();
            Folder c = new Folder();
            a.Name = "a";
            b.Name = "b";
            c.Name = "c";
            b.Parent = a;
            c.Parent = b;
            Category category = new Category() { ID = 1, Name = "Services" };
            a.Categories.Add(category);
            category.Folders.Add(a);
            Assert.IsTrue(c.IsInCategory("Services"));
        }

        [TestMethod]
        public void GetVirtualPath()
        {
            Folder a = new Folder();
            Folder b = new Folder();
            Folder c = new Folder();
            a.Name = "a";
            b.Name = "b";
            c.Name = "c";
            b.Parent = a;
            c.Parent = b;
            Assert.AreEqual("a", a.GetVirtualPath());
            Assert.AreEqual("a/b", b.GetVirtualPath());
            Assert.AreEqual("a/b/c", c.GetVirtualPath());
        }

        [TestMethod]
        public void IsParentTo()
        {
            Folder a = new Folder();
            Folder b = new Folder();
            Folder c = new Folder();
            b.Parent = a;
            c.Parent = b;
            Assert.IsTrue(a.IsParentTo(c));
            Assert.IsTrue(b.IsParentTo(c));
            Assert.IsFalse(c.IsParentTo(a));
        }

        [TestMethod]
        public void AllowsAdministrator_PermissionGrantedByDefaultAccessAllow_ReturnTrue()
        {
            Folder folder = new Folder();
            AdministratorGroup group = new AdministratorGroup();
            group.DefaultPageAccess = DefaultAccess.Allow;
            Administrator admin = new Administrator();
            admin.Group = group;
            Assert.IsTrue(folder.AllowsAdministrator(admin));
        }

        [TestMethod]
        public void AllowsAdministrator_PermissionDeniedByDefaultAccessDeny_ReturnFalse()
        {
            Folder folder = new Folder();
            AdministratorGroup group = new AdministratorGroup();
            group.DefaultPageAccess = DefaultAccess.Deny;
            Administrator admin = new Administrator();
            admin.Group = group;
            Assert.IsFalse(folder.AllowsAdministrator(admin));
        }

        [TestMethod]
        public void AllowsAdministrator_PermissionDeniedExplicitly_ReturnFalse()
        {
            Folder folder = new Folder();
            AdministratorGroup group = new AdministratorGroup();
            group.DefaultPageAccess = DefaultAccess.Allow;
            Administrator admin = new Administrator();
            admin.Group = group;
            folder.DeniedAdministratorGroups.Add(group);
            Assert.IsFalse(folder.AllowsAdministrator(admin));
        }

        [TestMethod]
        public void AllowsAdministrator_PermissionAllowedExplicitly_ReturnTrue()
        {
            Folder folder = new Folder();
            AdministratorGroup group = new AdministratorGroup();
            group.DefaultPageAccess = DefaultAccess.Deny;
            Administrator admin = new Administrator();
            admin.Group = group;
            folder.AllowedAdministratorGroups.Add(group);
            Assert.IsTrue(folder.AllowsAdministrator(admin));
        }

        [TestMethod]
        public void AllowsAdministrator_PermissionDeniedExplicitlyAndInherited_ReturnFalse()
        {
            Folder parent = new Folder();
            Folder folder = new Folder();
            folder.Parent = parent;
            parent.SubFolders.Add(folder);
            AdministratorGroup group = new AdministratorGroup();
            group.DefaultPageAccess = DefaultAccess.Allow;
            Administrator admin = new Administrator();
            admin.Group = group;
            parent.DeniedAdministratorGroups.Add(group);
            Assert.IsFalse(folder.AllowsAdministrator(admin));
        }

        [TestMethod]
        public void AllowsAdministrator_PermissionAllowedExplicitlyAndInherited_ReturnTrue()
        {
            Folder parent = new Folder();
            Folder folder = new Folder();
            folder.Parent = parent;
            parent.SubFolders.Add(folder);
            AdministratorGroup group = new AdministratorGroup();
            group.DefaultPageAccess = DefaultAccess.Deny;
            Administrator admin = new Administrator();
            admin.Group = group;
            parent.AllowedAdministratorGroups.Add(group);
            Assert.IsTrue(folder.AllowsAdministrator(admin));
        }

        [TestMethod]
        public void AllowsAdministrator_PermissionDeniedExplicitlyButOverriddenByChild_ReturnTrue()
        {
            Folder folder = new Folder();
            Folder subFolder = new Folder();
            Page page = new Page();
            subFolder.Parent = folder;
            folder.SubFolders.Add(subFolder);
            page.Folder = subFolder;
            subFolder.Pages.Add(page);
            AdministratorGroup group = new AdministratorGroup();
            group.DefaultPageAccess = DefaultAccess.Allow;
            Administrator admin = new Administrator();
            admin.Group = group;
            folder.DeniedAdministratorGroups.Add(group);
            page.AllowedAdministratorGroups.Add(group);
            Assert.IsTrue(folder.AllowsAdministrator(admin));
        }
    }
}
